<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends MX_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->model ( 'admin_model' );
	}
	
	public function authenticate() {
		$loggedSessionData = array ('admin' => null, 'sessionKey' => null, 'logged_in' => FALSE );
		$this->session->set_userdata ( $loggedSessionData );
		$this->load->view ( 'login' );
	}
	
	public function login() {
		$username = $this->input->post ( 'username' );
		$password = $this->input->post ( 'password' );
		$sessionKey = $this->input->post ( 'formKey' );
		$success = $this->admin_model->login ( $username, $password );
		
		if ($success) {
			$loggedSessionData = array ('admin' => $this->admin_model->getUsername(), 'sessionKey' => $sessionKey, 'logged_in' => TRUE );
			$this->session->set_userdata ( $loggedSessionData );
			redirect(site_url("admin/catalog/category/index", true));
		} else {
			redirect(site_url("admin/catalog/category/index", true));
			$this->session->set_flashdata('error', 'Username not exists or password not matched');
			redirect(site_url("admin/authenticate"));
		}
	}
	
	function index()
	{
		$this->load->view('welcome_message');
	}
	
	function dashboard() 
	{
		$this->load->view('dashboard');
	}
}
